// 初一数学知识点数据
export const mathTopics = [
  {
    id: 'rational-numbers',
    name: '有理数',
    category: '数与代数',
    difficulty: 2,
    description: '正数、负数、零的概念及运算',
    keyPoints: [
      '正数、负数、零的概念',
      '数轴上的表示',
      '相反数和绝对值',
      '有理数的加减乘除运算',
      '有理数的比较大小'
    ],
    commonMistakes: [
      '正负号混淆',
      '绝对值概念不清',
      '运算顺序错误',
      '分数运算错误'
    ],
    examples: [
      {
        question: '计算：(-3) + 5 - (-2)',
        answer: '4',
        explanation: '先算括号内的相反数，再从左到右计算'
      },
      {
        question: '|-7| + |3| = ?',
        answer: '10',
        explanation: '绝对值都是正数，|-7|=7, |3|=3'
      }
    ]
  },
  {
    id: 'algebraic-expressions',
    name: '代数式',
    category: '数与代数',
    difficulty: 3,
    description: '用字母表示数，代数式的运算',
    keyPoints: [
      '用字母表示数',
      '代数式的概念',
      '同类项的合并',
      '去括号法则',
      '代数式的值'
    ],
    commonMistakes: [
      '同类项识别错误',
      '去括号时符号错误',
      '代入求值时计算错误',
      '字母系数处理错误'
    ],
    examples: [
      {
        question: '化简：2x + 3y - x + 2y',
        answer: 'x + 5y',
        explanation: '同类项合并：2x-x=x, 3y+2y=5y'
      },
      {
        question: '当x=2, y=-1时，求3x-2y的值',
        answer: '8',
        explanation: '代入：3×2-2×(-1)=6+2=8'
      }
    ]
  },
  {
    id: 'equations',
    name: '一元一次方程',
    category: '数与代数',
    difficulty: 3,
    description: '方程的概念、解法和应用',
    keyPoints: [
      '方程的概念',
      '等式的性质',
      '移项法则',
      '系数化为1',
      '方程的应用'
    ],
    commonMistakes: [
      '移项时符号错误',
      '系数化为1时计算错误',
      '应用题列方程错误',
      '检验答案时遗漏'
    ],
    examples: [
      {
        question: '解方程：3x + 5 = 2x + 8',
        answer: 'x = 3',
        explanation: '移项：3x-2x=8-5, x=3'
      },
      {
        question: '一个数的3倍比这个数大10，求这个数',
        answer: '5',
        explanation: '设这个数为x，列方程：3x-x=10, 2x=10, x=5'
      }
    ]
  },
  {
    id: 'inequalities',
    name: '一元一次不等式',
    category: '数与代数',
    difficulty: 3,
    description: '不等式的概念、解法和应用',
    keyPoints: [
      '不等式的概念',
      '不等式的性质',
      '不等式的解法',
      '不等式组的解法',
      '不等式的应用'
    ],
    commonMistakes: [
      '不等式两边同乘负数时不等号方向错误',
      '不等式组解集表示错误',
      '应用题列不等式错误',
      '解集在数轴上表示错误'
    ],
    examples: [
      {
        question: '解不等式：2x - 3 > 5',
        answer: 'x > 4',
        explanation: '移项：2x > 8, 系数化为1：x > 4'
      },
      {
        question: '解不等式组：{x > 2, x < 5}',
        answer: '2 < x < 5',
        explanation: '两个不等式的交集'
      }
    ]
  },
  {
    id: 'geometry-basics',
    name: '几何基础',
    category: '图形与几何',
    difficulty: 2,
    description: '点、线、面、角的基本概念',
    keyPoints: [
      '点、线、面的概念',
      '角的概念和分类',
      '角的度量',
      '角的运算',
      '平行线和垂直线'
    ],
    commonMistakes: [
      '角度的计算错误',
      '平行线判定错误',
      '垂直线判定错误',
      '角度单位混淆'
    ],
    examples: [
      {
        question: '一个角的补角是60°，求这个角',
        answer: '120°',
        explanation: '补角和为180°，180°-60°=120°'
      },
      {
        question: '两个角互为余角，一个角是30°，求另一个角',
        answer: '60°',
        explanation: '余角和为90°，90°-30°=60°'
      }
    ]
  },
  {
    id: 'triangles',
    name: '三角形',
    category: '图形与几何',
    difficulty: 3,
    description: '三角形的性质、分类和计算',
    keyPoints: [
      '三角形的概念',
      '三角形的分类',
      '三角形的内角和',
      '三角形的外角和',
      '三角形的面积'
    ],
    commonMistakes: [
      '三角形内角和计算错误',
      '三角形分类错误',
      '面积公式应用错误',
      '三角形存在性判断错误'
    ],
    examples: [
      {
        question: '三角形三个内角分别是60°、70°、50°，求外角和',
        answer: '360°',
        explanation: '三角形外角和恒等于360°'
      },
      {
        question: '等腰三角形底角是40°，求顶角',
        answer: '100°',
        explanation: '内角和180°，180°-40°×2=100°'
      }
    ]
  },
  {
    id: 'quadrilaterals',
    name: '四边形',
    category: '图形与几何',
    difficulty: 3,
    description: '四边形的性质、分类和计算',
    keyPoints: [
      '四边形的概念',
      '平行四边形的性质',
      '矩形的性质',
      '菱形的性质',
      '正方形的性质'
    ],
    commonMistakes: [
      '四边形分类错误',
      '性质应用错误',
      '面积计算错误',
      '周长计算错误'
    ],
    examples: [
      {
        question: '平行四边形对角线互相平分，对边相等',
        answer: '正确',
        explanation: '这是平行四边形的基本性质'
      },
      {
        question: '矩形的对角线相等且互相平分',
        answer: '正确',
        explanation: '这是矩形的基本性质'
      }
    ]
  },
  {
    id: 'circles',
    name: '圆',
    category: '图形与几何',
    difficulty: 3,
    description: '圆的基本概念和计算',
    keyPoints: [
      '圆的概念',
      '圆心、半径、直径',
      '圆的周长',
      '圆的面积',
      '弧长和扇形面积'
    ],
    commonMistakes: [
      '半径和直径混淆',
      '周长和面积公式错误',
      'π值使用错误',
      '单位换算错误'
    ],
    examples: [
      {
        question: '圆的半径是5cm，求周长',
        answer: '10π cm',
        explanation: '周长=2πr=2×π×5=10π'
      },
      {
        question: '圆的直径是8cm，求面积',
        answer: '16π cm²',
        explanation: '半径=4cm，面积=πr²=π×4²=16π'
      }
    ]
  },
  {
    id: 'data-analysis',
    name: '数据分析',
    category: '统计与概率',
    difficulty: 2,
    description: '数据的收集、整理和分析',
    keyPoints: [
      '数据的收集',
      '数据的整理',
      '平均数、中位数、众数',
      '极差和方差',
      '统计图表'
    ],
    commonMistakes: [
      '平均数计算错误',
      '中位数确定错误',
      '众数识别错误',
      '统计图表解读错误'
    ],
    examples: [
      {
        question: '数据：2, 4, 6, 8, 10，求平均数',
        answer: '6',
        explanation: '平均数=(2+4+6+8+10)÷5=30÷5=6'
      },
      {
        question: '数据：1, 3, 5, 7, 9，求中位数',
        answer: '5',
        explanation: '奇数个数据，中位数是中间的数'
      }
    ]
  },
  {
    id: 'probability',
    name: '概率',
    category: '统计与概率',
    difficulty: 3,
    description: '概率的基本概念和计算',
    keyPoints: [
      '概率的概念',
      '随机事件',
      '概率的计算',
      '概率的应用',
      '树状图'
    ],
    commonMistakes: [
      '概率计算错误',
      '事件判断错误',
      '树状图绘制错误',
      '概率应用错误'
    ],
    examples: [
      {
        question: '抛一枚硬币，正面朝上的概率',
        answer: '1/2',
        explanation: '硬币有两个面，每个面概率相等'
      },
      {
        question: '从1-10中随机选一个数，选到偶数的概率',
        answer: '1/2',
        explanation: '偶数有5个，总数10个，概率=5/10=1/2'
      }
    ]
  }
]

// 错题本数据结构
export const mistakeBook = {
  mistakes: [],
  addMistake(topicId, question, userAnswer, correctAnswer, explanation) {
    this.mistakes.push({
      id: Date.now(),
      topicId,
      question,
      userAnswer,
      correctAnswer,
      explanation,
      date: new Date().toISOString(),
      reviewed: false
    })
    // 保存到本地存储
    localStorage.setItem('mathMistakeBook', JSON.stringify(this.mistakes))
  },
  getMistakes() {
    // 从本地存储加载
    const saved = localStorage.getItem('mathMistakeBook')
    if (saved) {
      this.mistakes = JSON.parse(saved)
    }
    return this.mistakes
  },
  removeMistake(id) {
    this.mistakes = this.mistakes.filter(m => m.id !== id)
    localStorage.setItem('mathMistakeBook', JSON.stringify(this.mistakes))
  },
  markAsReviewed(id) {
    const mistake = this.mistakes.find(m => m.id === id)
    if (mistake) {
      mistake.reviewed = true
      localStorage.setItem('mathMistakeBook', JSON.stringify(this.mistakes))
    }
  },
  getMistakesByTopic(topicId) {
    return this.mistakes.filter(m => m.topicId === topicId)
  }
}

// 获取所有数学主题
export function getAllMathTopics() {
  return mathTopics
}

// 按类别获取数学主题
export function getMathTopicsByCategory(category) {
  return mathTopics.filter(topic => topic.category === category)
}

// 按难度获取数学主题
export function getMathTopicsByDifficulty(difficulty) {
  return mathTopics.filter(topic => topic.difficulty === difficulty)
}

// 获取所有类别
export function getMathCategories() {
  return [...new Set(mathTopics.map(topic => topic.category))]
}

// 获取随机练习题
export function getRandomPracticeQuestions(topicId, count = 5) {
  const topic = mathTopics.find(t => t.id === topicId)
  if (!topic) return []
  
  const questions = []
  const examples = topic.examples
  
  // 基于知识点生成更多练习题
  for (let i = 0; i < count; i++) {
    const example = examples[i % examples.length]
    questions.push({
      ...example,
      id: `q_${topicId}_${i}`,
      topicId: topicId
    })
  }
  
  return questions
}

// 生成易错题练习
export function getCommonMistakeQuestions(topicId) {
  const topic = mathTopics.find(t => t.id === topicId)
  if (!topic) return []
  
  return topic.commonMistakes.map((mistake, index) => ({
    id: `mistake_${topicId}_${index}`,
    topicId: topicId,
    type: 'mistake',
    question: `关于"${mistake}"的练习`,
    explanation: `这是${topic.name}中的常见错误，需要特别注意`,
    difficulty: topic.difficulty + 1
  }))
} 